package model.run;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.swing.JFrame;

import mode.enumeration.Direction;
import model.listener.CheckPointListener;
import model.vo.Logging;
import utils.NoLimeUtils;
import controller.NLSimulator;
import controller.view.NLConstants;
import controller.view.impl.NLSimulatorSwingView;

public class Treino2 extends JFrame {

	public static Long start = Long.valueOf(-1);
	NLSimulator transportPlan = new NLSimulator(0, 0);
	protected List<NLSimulator> tps = new ArrayList<NLSimulator>();
	
	public static Long speed = 4l;

	CheckPointListener cp1 = new CheckPointListener() {
		public void reached(NLSimulator nls) {
			logLastRecord(nls);
			
			int i=0;
			do {
				Random random=new Random();
				i=(int)(random.nextGaussian()*10);			
			}while(i<5||i>10);
			
			nls.setDefaultSpeed((long)i);
			
			NoLimeUtils.predictMMXMQVerbose(nls, 1);
			
		}
	};
	
	CheckPointListener cp2 = new CheckPointListener() {
		public void reached(NLSimulator nls) {
			logLastRecord(nls);			
			NoLimeUtils.predictMQVerbose(nls, 2);
		}
	};
	
	CheckPointListener cp3 = new CheckPointListener() {
		public void reached(NLSimulator nls) {
			logLastRecord(nls);			
			NoLimeUtils.predictMQVerbose(nls, 3);
		}
	};
	
	CheckPointListener cp4 = new CheckPointListener() {
		public void reached(NLSimulator nls) {
			logLastRecord(nls);			
			NoLimeUtils.predictMQVerbose(nls, 4);
		}
	};
	
	CheckPointListener cp5 = new CheckPointListener() {
		public void reached(NLSimulator nls) {
			logLastRecord(nls);			
			NoLimeUtils.predictMQVerbose(nls, 5);
		}
	};
	
	CheckPointListener cp6 = new CheckPointListener() {
		public void reached(NLSimulator nls) {
			logLastRecord(nls);			
			NoLimeUtils.predictMQVerbose(nls, 6);
		}
	};
	
	public Treino2(){
		
		transportPlan.setDefaultSpeed(20l);
		transportPlan.setLoop(true);

		tps.add(transportPlan);
		commands();
		initialize();
		
	}
	
	public void commands(){
		
		transportPlan.addCommand(Direction.HORIZONTAL, NLConstants.REF_BASE * 3);
		transportPlan.addCommand(Direction.VERTICAL, NLConstants.REF_BASE * 1);
		transportPlan.addCommand(Direction.HORIZONTAL, NLConstants.REF_BASE * 1);
		transportPlan.addCommand(Direction.VERTICAL, NLConstants.REF_BASE * 3);
		transportPlan.addCommand(Direction.HORIZONTAL, NLConstants.REF_BASE * -4);
		transportPlan.addCommand(Direction.VERTICAL, NLConstants.REF_BASE * -4);
	}
	
	public void initialize(){
		
		transportPlan.addCheckPoint(NLConstants.REF_BASE * 1, NLConstants.REF_BASE * 0, cp1);
		transportPlan.addCheckPoint(NLConstants.REF_BASE * 2, NLConstants.REF_BASE * 0, cp1);
		transportPlan.addCheckPoint(NLConstants.REF_BASE * 3, NLConstants.REF_BASE * 0, cp1);
		transportPlan.addCheckPoint(NLConstants.REF_BASE * 4, NLConstants.REF_BASE * 3, cp1);
		transportPlan.addCheckPoint(NLConstants.REF_BASE * 3, NLConstants.REF_BASE * 1, cp1);
		transportPlan.addCheckPoint(NLConstants.REF_BASE * 4, NLConstants.REF_BASE * 1, cp1);
		transportPlan.addCheckPoint(NLConstants.REF_BASE * 4, NLConstants.REF_BASE * 2, cp1);
		transportPlan.addCheckPoint(NLConstants.REF_BASE * 4, NLConstants.REF_BASE * 3, cp1);
		transportPlan.addCheckPoint(NLConstants.REF_BASE * 4, NLConstants.REF_BASE * 4, cp1);
		transportPlan.addCheckPoint(NLConstants.REF_BASE * 3, NLConstants.REF_BASE * 4, cp1);
		transportPlan.addCheckPoint(NLConstants.REF_BASE * 2, NLConstants.REF_BASE * 4, cp1);
		transportPlan.addCheckPoint(NLConstants.REF_BASE * 1, NLConstants.REF_BASE * 4, cp1);
		transportPlan.addCheckPoint(NLConstants.REF_BASE * 0, NLConstants.REF_BASE * 4, cp1);
		transportPlan.addCheckPoint(NLConstants.REF_BASE * 0, NLConstants.REF_BASE * 3, cp1);
		transportPlan.addCheckPoint(NLConstants.REF_BASE * 0, NLConstants.REF_BASE * 2, cp1);
		transportPlan.addCheckPoint(NLConstants.REF_BASE * 0, NLConstants.REF_BASE * 1, cp1);
		transportPlan.addCheckPoint(NLConstants.REF_BASE * 0, NLConstants.REF_BASE * 0, cp1);
	}
	
	public void startSimulation(){
		new NLSimulatorSwingView(tps);
	}
	
	public void logLastRecord(NLSimulator nls){
		if(nls.getLogger().size()!=1){
			Logging log=((List<Logging>)nls.getLogger()).get(nls.getLogger().size()-1);
			Logging logPrior=((List<Logging>)nls.getLogger()).get(nls.getLogger().size()-2);
			
//			if(NoLimeUtils.subtract(log.getPosition(), logPrior.getPosition())==0){
//				System.out.println();
//				return;
//			}
			
			String valor=""+((double)(log.getTime()-logPrior.getTime())/1000);
			System.out.println(valor);
		}
	}
	
	public static void main(String... args) {

		Treino2 t2=new Treino2();
		t2.startSimulation();

	}

}
